package com.example.machine.controller;

import com.example.machine.constant.Constants;
import com.example.machine.model.UserInfo;
import com.example.machine.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Home-pc
 */
@RestController
@Slf4j
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/login")
    public Boolean login(String userName, String password, HttpSession session){
        //校验参数
        if(!StringUtils.hasLength(userName)  || !StringUtils.hasLength(password)){
            log.info("校验参数错误");
            return false;
        }
        //验证账号密码是否正确
        // 1.根据前端传过来的用户名和数据库中的用户名进行对比，调用service
            UserInfo userInfo = userService.getUserInfoByName(userName);
        // 2.和数据库中的密码对比，是否正确
        //传进来的用户为空或者传经来的用户不存在
        if(userInfo == null || userInfo.getId() <= 0){
            log.info("用户不存在");
            return false;
        }
        if(password.equals(userInfo.getPassword())){
            //密码正确，存session
            userInfo.setPassword("");
            session.setAttribute(Constants.SESSION_USER_KEY,userInfo);
            return true;
        }
        return false;
    }










}
